﻿using System;
using System.Windows;
using System.Windows.Controls;
using Dsl.Ped.Framework.Common;
using Dsl.Ped.Framework.Projections.LayoutProjections;

namespace Dsl.Ped.Framework.Projections
{
    /// <summary>
    /// Factory for the creation of layout-based projections
    /// </summary>
    public class LayoutProjectionFactory : ProjectionFactoryBase
    {
        /// <summary>
        /// Initialize the projection control of the Panel-based projection (StackPanel, DockPanel,...)
        /// </summary>
        /// <typeparam name="T">Type of the projection (like Grid, StackPanel, etc.)</typeparam>
        /// <param name="projectionHint">Hint for the creation of projection</param>
        /// <param name="projections">Projections organized in a specific layout</param>
        /// <returns><see cref="FrameworkElement"/> organizing given projections in a required layout</returns>
        public static FrameworkElement InitializeLayoutProjection<T>(
            ILayoutProjectionHint<T> projectionHint, params IProjection[] projections) where T : Panel, new()
        {
            var panelProjection = InitializeProjection(projectionHint, null);
            return panelProjection;
        }

        #region StackPanel

        /// <summary>
        /// Get the projection, that consists of several projection organized in the <see cref="StackPanel"/>
        /// </summary>
        /// <param name="customConfiguration">Custom configuration action to configure the <see cref="StackPanel"/></param>
        /// <param name="projections">Projections to be organized in the <see cref="StackPanel"/></param>
        /// <returns>Projections organized to the <see cref="StackPanel"/></returns>
        public static IProjection GetStackPanelProjection(Action<StackPanel> customConfiguration, params IProjection[] projections)
        {
            var projectionHint = new LayoutProjectionHint<StackPanel>(
                (stackpanel, type) =>
                    { 
                        stackpanel.Orientation = Orientation.Horizontal;
                        foreach (var projection in projections)
                        {
                            stackpanel.Children.Add(projection.View);
                        }
                    },
                customConfiguration);
            
            return new Projection() { View = InitializeLayoutProjection(projectionHint, projections) };
        }

        /// <summary>
        /// Get the projection, that consists of several projection organized in the <see cref="StackPanel"/>
        /// </summary>
        /// <param name="projections">Projections to be organized in the <see cref="StackPanel"/></param>
        /// <returns>Projections organized to the <see cref="StackPanel"/></returns>
        public static IProjection GetStackPanelProjection(params IProjection[] projections)
        {
            return GetStackPanelProjection(null, projections);
        }

        #endregion
    }
}
